package com.teatrace.commons.taglib;
import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.teatrace.commons.taglib.exception.JumpTagException;



public class PageTag extends TagSupport {
	
	
	private String action;
	
	private String property;
	
	private String page;
	
	private String total;
	
	private static final String style = "<!-- 自动分页代码  start -->\n" +
		"<style>" +
		".pagination, .pagination a {" +
		"	margin-left: 30px;" +
		"	font-size: 11px;" +
		"	font-weight: nomal;" +
		"}" +
		".pagination a {" +
		"	text-decoration: underline;" +
		"}" +
		"</style>";
	
	private static String template = "" +
		style +
		"<div class='pagination'>\n" +
		"<a href='#start_href'>首页</a>\n" +
		"<a href='#last_href'>上一页</a>" +
		"\n#pages" + 
		"<a href='#next_href'>下一页</a>\n" +
		"<a href='#end_href'>尾页</a>\n" +
		"&nbsp;共&nbsp;#total&nbsp;页\n" +
		"</div>" +
		"<!-- 自动分页代码  end -->\n";

	@Override
	public int doEndTag() throws JspException {
		
		return EVAL_PAGE;
	}
	
	@Override
	public int doStartTag(){
		try {
			pageContext.getOut().write(toHtml());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			throw new JumpTagException("the tag <jump:paga> attributes can't filled with null or empty!");
		}
		return EVAL_BODY_INCLUDE;
    }
	
	public String toHtml() {
		String html = template;
		int page = Integer.parseInt(this.page);
		int total = Integer.parseInt(this.total);
		int start = 1;
		int end = 1;
		if(page <= 0) {
			throw new RuntimeException("page must great than 0 !!!");
		}
		if(total == 0) {
			return "<!-- 自动分页代码  start --><!-- total=0 --><!-- 自动分页代码  end -->";
		}
		if(page > total) {
			throw new RuntimeException("page must less than total page " + total);
		}
		if(page > 0 && page < 4) {
			start = 1;
			end = 5;
		}
		if(page + 2 >= total) {
			if(total > 5) {
				start = total - 5;
				end = total;
			} else {
				start = 1;
				end = total;
			}
		}
		if(page == 1) {
			html = html.replace("<a href='#start_href'>首页</a>", "");
		}
		if(page == total) {
			html = html.replace("<a href='#end_href'>尾页</a>", "");
		}
		if(start == 1) {
			html = html.replace("<a href='#last_href'>上一页</a>", "");
		} 
		if(end == total) {
			html = html.replace("<a href='#next_href'>下一页</a>", "");
			
		}
		if(total == 1) {
			return style + "<div class='pagination'><b>共&nbsp;1&nbsp;页</b></div>";
		}
		
		
		System.out.println("start:" + start + " end:" + end);
		
		html = html.replace("#start_href", getHref(1));
		html = html.replace("#last_href", getHref(page - 1));
		for(int i=start; i<=(end > total ? total : end); i++) {
			html = html.replace("#pages", "<a href='" + getHref(i) + "'>" + i + "</a>\n#pages");
			if(i == page) {
				html = html.replace("<a href='" + getHref(i) + "'>" + i + "</a>", "<b>" + i + "</b>");
			}
		}
		html = html.replace("#pages", "");
		html = html.replace("#next_href", getHref(page + 1));
		html = html.replace("#end_href", getHref(total));
		html = html.replace("#total", total + "");
		return html;
	}
	
	private String getHref(int page) {
		return action + "?" + property + "=" + page;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	
}
